﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Ymatou.API.Common
{
    /// <summary>
    /// 默认客户端调用代码
    /// </summary>
    [Obsolete("推荐只用ApiClient. 此类型不会再更新")]
    public class DefaultClient : IClient
    {
        /// <summary>
        /// 请求超时时间，单位毫秒
        /// </summary>
        public int Timeout
        {
            get { return this._timeout; }
            set { this._timeout = value; }
        }

        /// <summary>
        /// 服务地址，通常为http://域名.
        /// </summary>
        public string ServerUrl { get; set; }


        /// <param name="serverUrl">服务地址，通常为http://域名</param>
        public DefaultClient(string serverUrl)
        {
            ServerUrl = serverUrl;
        }

        /// <param name="serverUrl">服务地址，通常为http://域名</param>
        /// <param name="timeout">请求超时时间，单位毫秒</param>
        public DefaultClient(string serverUrl, int timeout)
        {
            ServerUrl = serverUrl;
            Timeout = timeout;
        }

        /// <summary>
        /// 执行请求. 此时ServerUrl不支持复杂格式.
        /// </summary>
        /// <param name="jsonStr">必须为json字符串，否则出错</param>
        public object Execute(string apiName, string jsonStr, bool httpGet)
        {
            return Execute(apiName, jsonStr, httpGet, null, false);
        }

        /// <summary>
        /// 执行请求. 此时ServerUrl不支持复杂格式.
        /// </summary>
        /// <param name="jsonStr">请求数据</param>
        /// <param name="httpGet">请求方式</param>
        /// <param name="proxy">默认传null</param>
        /// <param name="useNagleAlgorithm">默认传false</param>
        public object Execute(string apiName, string jsonStr, bool httpGet, IWebProxy proxy, bool useNagleAlgorithm)
        {
            if (string.IsNullOrEmpty(ServerUrl))
            {
                throw new ArgumentException("ServerUrl不能为空");
            }
            if (string.IsNullOrEmpty(apiName))
            {
                throw new ArgumentException("ApiName不能为空");
            }
            //
            string responseBody = ApiClient.Execute(apiName, httpGet ? JObject.Parse(jsonStr) as object : jsonStr as object, this.ServerUrl, httpGet, this.Timeout, proxy, useNagleAlgorithm);

            return JsonConvert.DeserializeObject(responseBody);
        }

        /// <summary>
        /// 执行请求. 此时ServerUrl支持复杂格式.
        /// </summary>
        /// <typeparam name="T">Response类型</typeparam>
        /// <param name="request">请求实体</param>
        public BaseResponseModel<T> Execute<T>(BaseRequestModel<T> request) where T : IResponse
        {
            return Execute(request, null, false);
        }

        /// <summary>
        /// 执行请求. 此时ServerUrl支持复杂格式.
        /// </summary>
        /// <typeparam name="T">Response类型</typeparam>
        /// <param name="request">请求</param>
        /// <param name="proxy">设置代理. 默认传null</param>
        /// <param name="useNagleAlgorithm">是否启用Nagling Algorithm算法.该算法对于提交数据量小的Post请求可能会带来约200ms的延迟. 默认传false</param>
        public BaseResponseModel<T> Execute<T>(BaseRequestModel<T> request, IWebProxy proxy, bool useNagleAlgorithm) where T : IResponse
        {
            return ApiClient.Execute(request, this.ServerUrl, this.Timeout, proxy, useNagleAlgorithm);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public BaseResponseModel<T> Execute<T>(IFileRequest<T> request) where T : IFileResponse
        {
            return null;
        }


        private int _timeout = 100000;
    }
}